package com.gitee.chili.leisure.service;

import com.gitee.chili.leisure.RoomManager;
import com.gitee.chili.leisure.core.GameProcess;
import com.gitee.chili.leisure.core.GameTemplate;
import com.gitee.chili.leisure.game.Player;
import com.gitee.chili.leisure.game.RoomDetail;
import com.gitee.chili.leisure.util.AssistMahjong;
import com.gitee.chili.leisure.util.Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * MahjongService
 *
 * @author week solider
 * @since 2020.09.17
 */
@Slf4j
@Service
public class MahjongService implements GameProcess, SCMahjongService {

    @Autowired
    private GameTemplate gameTemplate;

    @Override
    public void begin(Long roomNum) {
        List<Integer> cards = AssistMahjong.generateCards();

        // 玩家列表
        RoomDetail roomDetail = RoomManager.obtain(roomNum);
        List<? extends Player> players = roomDetail.getPlayers();

        // 洗牌
        gameTemplate.shuffle(1, cards, roomDetail);
        // 选庄
        gameTemplate.election(players);
        // 发牌
        gameTemplate.deal(cards, players, roomDetail);
        // 排序
        gameTemplate.sort(players);

        // 定义庄家座位 (也是第一个出牌的玩家)
        Utils.accept(players, Player::isBanker, roomDetail::defineBanker);
    }

    @Override
    public void changingCards() {

    }

    @Override
    public void useless() {

    }

    @Override
    public void over() {

    }

}
